package controller;

import model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import service.UserService;

@Controller
@RequestMapping(value = "user")
public class UserController extends BaseController{
    private UserService userService;

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    //注册
    @RequestMapping(value = "signUp",method = RequestMethod.POST)
    private String createUser(User user){
        userService.create(user);
        session.setAttribute("message","注册成功！");
        return "redirect:/index.jsp";
    }
    //登录
    @RequestMapping(value = "signIn",method = RequestMethod.POST)
    private String signIn(User user){
        user = (User) userService.query("queryUserByUsernameAndPassword",user);
        if (user == null){
            session.setAttribute("error","用户名和密码错误！");
        }else {
            session.setAttribute("user",user);
        }
        return "redirect:/index.jsp";
    }
    //退出登录
    @RequestMapping(value = "signOut",method = RequestMethod.GET)
    private String signOut(){
        session.invalidate();
        return "redirect:/index.jsp";
    }
}
